﻿// 导航逻辑多边形区块
using System;
using System.Collections.Generic;

namespace ShawnNav
{
    public class NavPoly : IComparable<NavPoly>
    {
        public int areaID;
        public int[] indexArr;
        public NavVector3[] verticeArr;

        // 包围盒数据（用于区块检索加速）
        public NavVector3 min = new NavVector3(float.MaxValue, float.MaxValue, float.MaxValue);
        public NavVector3 max = new NavVector3(float.MinValue, float.MinValue, float.MinValue);
        public NavVector3 center = NavVector3.Zero;

        // 区块边界
        public List<NavBorder> borderLst;

        // 寻路数据
        public NavBorder targetBorder;
        public NavVector3 start = NavVector3.Zero;
        public float priority;
        public float sumDistance = float.PositiveInfinity;
        public NavPoly prePoly = null;

        public NavPoly(int areaID, int[] indexArr, NavVector3[] verticeArr)
        {
            this.areaID = areaID;
            this.indexArr = indexArr;
            this.verticeArr = verticeArr;

            // 初始化包围盒大小
            for (int i = 0; i < indexArr.Length; i++)
            {
                NavVector3 nv = verticeArr[indexArr[i]];
                if (nv.x < min.x) min.x = nv.x;
                if (nv.y < min.y) min.y = nv.y;
                if (nv.z < min.z) min.z = nv.z;
                if (nv.x > max.x) max.x = nv.x;
                if (nv.y > max.y) max.y = nv.y;
                if (nv.z > max.z) max.z = nv.z;

                center += nv;
            }

            center = center / indexArr.Length;
        }

        /// <summary>
        /// 求距离（求距离下一区块最近的点为起点多重寻路）
        /// </summary>
        /// <param name="neighborArea"></param>
        /// <returns></returns>
        public float CalcNavAreaDistancePoly(NavPoly neighborArea)
        {
            int[] indexArr = neighborArea.indexArr;
            float minSqrDis = float.MaxValue;
            for (int i = 0; i < indexArr.Length; i++)
            {
                NavVector3 nv = verticeArr[indexArr[i]];
                float newSqrDis = NavVector3.SqrDistance(start, nv);
                if (newSqrDis < minSqrDis)
                {
                    minSqrDis = newSqrDis;
                    neighborArea.start = nv;
                }
            }
            return minSqrDis; 
        }

        /// <summary>
        /// 基于中心点求距离（不建议，偏差较大）
        /// </summary>
        /// <param name="neighborArea"></param>
        /// <returns></returns>
        public float CalcNavAreaDistanceCenter(NavPoly neighborArea)
        {
            return NavVector3.SqrDistance(center, neighborArea.center);
        }

        public int CompareTo(NavPoly other)
        {
            if (priority < other.priority)
            {
                return -1;
            }else if (priority > other.priority)
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }

        public void Reset()
        {
            targetBorder = null;
            start = NavVector3.Zero;
            priority = 0;
            sumDistance = float.PositiveInfinity;
            prePoly = null;
        }
    }
}
